package com.webvihar.access2db.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.webvihar.access2db.R;
import com.webvihar.access2db.model.Authentication;
import com.webvihar.common.db.DbService;
import com.webvihar.common.utils.Validation;

public class RegisterActivity extends BaseActivity implements OnClickListener, TextWatcher {

	private DbService db;
	private EditText emailIdTxt;

	private EditText sc1Txt;
	private EditText sc2Txt;
	private EditText sc3Txt;
	private EditText sc4Txt;

	private EditText csc1Txt;
	private EditText csc2Txt;
	private EditText csc3Txt;
	private EditText csc4Txt;

	private Button submitBtn;
	private TextView titleTxt;
	private ImageButton addButton;

	/** Called when the activity is first created. **/
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.register);
		setTitleBar();
		initUI();
		initDbService();
		setListeners();
	}

	private void initUI() {
		sc1Txt = (EditText)findViewById(R.id.register_security_code1_et);
		sc2Txt = (EditText)findViewById(R.id.register_security_code2_et);
		sc3Txt = (EditText)findViewById(R.id.register_security_code3_et);
		sc4Txt = (EditText)findViewById(R.id.register_security_code4_et);
		csc1Txt = (EditText)findViewById(R.id.register_confirm_security_code1_et);
		csc2Txt = (EditText)findViewById(R.id.register_confirm_security_code2_et);
		csc3Txt = (EditText)findViewById(R.id.register_confirm_security_code3_et);
		csc4Txt = (EditText)findViewById(R.id.register_confirm_security_code4_et);
		emailIdTxt = (EditText)findViewById(R.id.register_emailid_et);
		submitBtn = (Button)findViewById(R.id.register_submit_btn);
	}

	private void initDbService() {
		db = new DbService(this);
	}

	private void setListeners() {
		submitBtn.setOnClickListener(this);
		sc1Txt.addTextChangedListener(this);
		sc2Txt.addTextChangedListener(this);
		sc3Txt.addTextChangedListener(this);
		sc4Txt.addTextChangedListener(this);
		csc1Txt.addTextChangedListener(this);
		csc2Txt.addTextChangedListener(this);
		csc3Txt.addTextChangedListener(this);
		csc4Txt.addTextChangedListener(this);
	}
	private void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.access2db_txt));
		addButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		addButton.setVisibility(View.GONE);
	}

	private void updateFocus(EditText prev, EditText cur, EditText next) {
		if(cur.getText().toString().length() == 1) {
			if(next != null)
				next.requestFocus();
		} else {
			if(prev != null)
				prev.requestFocus();
		}
	}

	public boolean saveAuthentication(String emailId, String securityCode) {
		Authentication auth = new Authentication(emailId, securityCode);
		db.Save(auth);
		return true;
	}

	@Override
	public void onClick(View v) {
		if(v == submitBtn) {
			
			String emailId = emailIdTxt.getText().toString();
			String securityCode = String.format("%s%s%s%s", sc1Txt.getText().toString(), sc2Txt.getText().toString(), sc3Txt.getText().toString(), sc4Txt.getText().toString());
			String confirmSecurityCode = String.format("%s%s%s%s", csc1Txt.getText().toString(), csc2Txt.getText().toString(), csc3Txt.getText().toString(), csc4Txt.getText().toString());
			if(Validation.isEmail(emailId) && securityCode.equalsIgnoreCase(confirmSecurityCode)) {
				if(saveAuthentication(emailId, securityCode)) {
					Intent intent =new Intent(RegisterActivity.this, SessionsActivity.class);
					startActivity(intent);
				}
			}
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		if(sc1Txt.hasFocus()) {
			updateFocus(null, sc1Txt, sc2Txt);
		} else if(sc2Txt.hasFocus()) {
			updateFocus(sc1Txt, sc2Txt, sc3Txt);
		} else if(sc3Txt.hasFocus()) {
			updateFocus(sc2Txt, sc3Txt, sc4Txt);
		} else if(sc4Txt.hasFocus()) {
			updateFocus(sc3Txt, sc4Txt, csc1Txt);
		} else if(csc1Txt.hasFocus()) {
			updateFocus(sc4Txt, csc1Txt, csc2Txt);
		} else if(csc2Txt.hasFocus()) {
			updateFocus(csc1Txt, csc2Txt, csc3Txt);
		} else if(csc3Txt.hasFocus()) {
			updateFocus(csc2Txt, csc3Txt, csc4Txt);
		} else if(csc3Txt.hasFocus()) {
			updateFocus(csc3Txt, csc4Txt, null);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {		
	}
	
}
